package esay;

class Solution {
    public int reverse(int x) {
        String xStr = x + "";
        if (x >= 0) {
            xStr = convert(xStr);
        } else {
            xStr = "-" + convert(xStr.substring(1));
        }
        long val = Long.valueOf(xStr);
        if(val>Integer.MAX_VALUE||val<Integer.MIN_VALUE){
            return 0;
        }
        return  (int)val;
    }

    /**
     * 翻转数字的方法。 我是通过数字转换成字符串再来翻转的方法。
     *
     * @param xStr
     * @return
     */
    public String convert(String xStr) {
        char[] xChars = xStr.toCharArray();
        int len = xChars.length;

        char[] vChars = new char[len];
        for (int i = 0; i < len; i++) {
            vChars[len - i - 1] = xChars[i];
        }
        return new String(vChars);
    }
}